package com.jie.myvhr.controller.emp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jie.myvhr.bean.Employee;
import com.jie.myvhr.bean.RespBean;
import com.jie.myvhr.common.EmailRunnable;
import com.jie.myvhr.common.poi.PoiUtils;
import com.jie.myvhr.service.DepartmentService;
import com.jie.myvhr.service.EmpService;
import com.jie.myvhr.service.JobLevelService;
import com.jie.myvhr.service.PositionService;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.TemplateEngine;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

/**
 * @author jie
 * @date 2019/6/30 -15:52
 */
@RestController
@RequestMapping("/employee/basic")
public class EmpBasicController {

    @Resource
    private EmpService empService;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private PositionService positionService;
    @Resource
    private JobLevelService jobLevelService;
    @Resource
    private ExecutorService executorService;
    @Resource
    private JavaMailSender javaMailSender;
    @Resource
    private TemplateEngine templateEngine;

    @RequestMapping(value = "/basicdata", method = RequestMethod.GET)
    public Map<String, Object> getAllNations() {
        Map<String, Object> map = new HashMap<>();
        //民族
        map.put("nations", empService.getAllNations());
        //政治面貌
        map.put("politics", empService.getAllPolitics());
        //请选择部门
        map.put("deps", departmentService.getDepByPid(-1L));
        //职位
        map.put("positions", positionService.getAllPos());
        //职称
        map.put("joblevels", jobLevelService.getAllJobLevels());
        //工号
        map.put("workID", String.format("%08d", empService.getMaxWorkID() + 1));
        return map;
    }

    /**
     * @param page           默认为1
     * @param size           默认为10
     * @param keywords       查询关键字 默认为空串
     * @param politicId      政治面貌
     * @param nationId       民族
     * @param posId          职位
     * @param jobLevelId     职称
     * @param engageForm     聘用形式
     * @param departmentId   部门
     * @param beginDateScope 入职日期查询
     */
    @RequestMapping(value = "/emp", method = RequestMethod.GET)
    public Map<String, Object> getEmployeeByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(defaultValue = "") String keywords,
            Long politicId, Long nationId, Long posId,
            Long jobLevelId, String engageForm,
            Long departmentId, String beginDateScope) {
        PageHelper.startPage(page, size);
        Map<String, Object> map = new HashMap<>();
        List<Employee> employeeByPage = empService.getEmployeeByPage(
                keywords, politicId, nationId, posId, jobLevelId, engageForm,
                departmentId, beginDateScope);
        PageInfo<Employee> pageInfo = new PageInfo<>(employeeByPage);
        map.put("emps", pageInfo.getList());
        map.put("count", pageInfo.getTotal());
        return map;
    }

    /**
     * 删除员工
     */
    @RequestMapping(value = "/emp/{ids}", method = RequestMethod.DELETE)
    public RespBean deleteEmpById(@PathVariable String ids) {
        if (empService.deleteEmpById(ids)) {
            return RespBean.ok("删除成功!");
        }
        return RespBean.error("删除失败!");
    }

    /**
     * 更新员工
     */
    @RequestMapping(value = "/emp", method = RequestMethod.PUT)
    public RespBean updateEmp(Employee employee) {
        if (empService.updateEmp(employee) == 1) {
            return RespBean.ok("更新成功!");
        }
        return RespBean.error("更新失败!");
    }

    /**
     * 新增员工信息
     */
    @RequestMapping(value = "/emp", method = RequestMethod.POST)
    public RespBean addEmp(Employee employee) {
        //新增
        if (empService.addEmp(employee) == 1) {
            //调用邮件发送的线程
            executorService.execute(new EmailRunnable(employee,
                    javaMailSender, templateEngine));
            return RespBean.ok("添加成功!");
        }
        return RespBean.error("添加失败!");
    }

    /**
     * 新增的时候查询最大的员工id，前台会把它作为新增员工时候的id
     */
    @RequestMapping("/maxWorkID")
    public String maxWorkID() {
        return String.format("%08d", empService.getMaxWorkID() + 1);
    }

    /**
     * 导出员工信息=========》》 根据查询的结果导出
     */
    @RequestMapping(value = "/exportEmp", method = RequestMethod.GET)
    public ResponseEntity<byte[]> exportEmp() {
        return PoiUtils.exportEmp2Excel(empService.getAllEmployees());
    }

    /**
     * 导入员工数据
     */
    @RequestMapping(value = "/importEmp", method = RequestMethod.POST)
    public RespBean importEmp(MultipartFile file) {
        List<Employee> emps = PoiUtils.importEmp2List(file,
                empService.getAllNations(), empService.getAllPolitics(),
                departmentService.getAllDeps(), positionService.getAllPos(),
                jobLevelService.getAllJobLevels());
        if (empService.addEmps(emps) == emps.size()) {
            return RespBean.ok("导入成功!");
        }
        return RespBean.error("导入失败!");
    }

}
